﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Qianli.DirSir.Common;
using System.Data.SqlClient;
using System.Data;
using LTSMZ.life.Model;

namespace LTSMZ.life.DAL
{
    public class ShoppingCarDal
    {
        SQLHelper helper = new SQLHelper();
        /// <summary>
        /// 购物车显示
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public System.Data.DataTable ShowShoppingCar(int id)
        {

            StringBuilder builder = new StringBuilder();
            builder.Append("select ShoppingCar.shoppingID,Product.Pid,Pname,PImg.PImgHref,Product.Price,ShoppingCar.ProNum from ShoppingCar  join Product on ShoppingCar.pid=Product.Pid join Member on ShoppingCar.mid=Member.Mid join PImg on Product.Pid=PImg.Pid join Stock on Stock.Pid=Product.pid where PImg.Pzhu=1 and Member.mid=@UserID");

            System.Data.SqlClient.SqlParameter[] parameter = new System.Data.SqlClient.SqlParameter[]{
            
              new System.Data.SqlClient.SqlParameter("@UserID",System.Data.SqlDbType.Int)   
               
            };
            parameter[0].Value = id;

            System.Data.DataTable table=helper.ExecDataTable(builder.ToString(), parameter);

            return table;
        }
        /// <summary>
        ///添加购物车
        /// </summary>
        /// <returns></returns>
        public int InsertShoppingCar(ShoppingCar car)
        {
            //判断该商品是否存在
            StringBuilder bulid = new StringBuilder();
            bulid.Append("select COUNT(*) from shoppingcar where pid=@pid");
            SqlParameter para = new SqlParameter("@pid", car.Pid);
            int i =Convert.ToInt32(helper.ExecScalar(bulid.ToString(), para));

            //如果大于零 证明改商品存在（在原来商品的数量上累加）  反之不存在，添加一个新商品
            if(i>0)
            {
                string str = "update shoppingcar set ProNum=ProNum+@num where Pid=@Pid ";
                SqlParameter[] param = new SqlParameter[]{
                    new SqlParameter("@num", car.ProNum),
                    new SqlParameter("@Pid", car.Pid)
                };
               
                helper.ExecNonquery(str, param);
                return 1;
            }
            else
            {
                //改商品不存在，可以添加
                StringBuilder builder = new StringBuilder();
                builder.Append("insert into shoppingCar(mid,pronum,pid,sumprice) values(@mid,@ProNum,@Pid,@SumPrice)");

                SqlParameter[] parameter = new SqlParameter[]{
                    new SqlParameter("@mid",car.Mid),
                    new SqlParameter("@ProNum",car.ProNum),
                    new SqlParameter("@Pid",car.Pid),
                    new SqlParameter("@SumPrice",car.SumPrice)
                };

                helper.ExecNonquery(builder.ToString(), parameter);
                return 2;
            }
        }
        /// <summary>
        /// 删除购物车
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int delShoppingCar(int id)
        {
            string str = "delete from ShoppingCar where shoppingID=@id";
            SqlParameter[] parameter = new SqlParameter[]{
                new SqlParameter("@id",id)
            };
            return helper.ExecNonquery(str, parameter);            
        }
    }
}
